import { ref } from "vue";

import { api } from "@/api";
import { useUserStore } from "@/stores/user";

export interface Login {
  username: string;
  password: string;
}

const user = useUserStore();

export function useLogin() {
  const loginModel = ref<Login>({
    username: "",
    password: "",
  });

  async function toLogin() {
    const res = (await api.post("/login", loginModel.value)).data;
    localStorage.setItem("token", res?.data?.token);
    await user.getUserInfo();
    return res?.msg;
  }

  return {
    loginModel,
    toLogin,
  };
}
